package pl.swspiz;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *  */

public class ListRecordsServlet extends HttpServlet {
    Connection currentCon = null;
    ResultSet rs = null;
   // @PersistenceUnit
    //private EntityManagerFactory emf;
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
 
    //PrintWriter out = response.getWriter();
    Statement stmt = null;    
    Unit unit;
    
    List<Unit> dataList = new ArrayList<Unit>();
      try {
      currentCon = ConnectionManager.getConnection();
      stmt = currentCon.createStatement();
      rs = stmt.executeQuery("select * from jednostki");

      while (rs.next()){
       
       unit = new Unit(rs.getString("nazwa"), rs.getFloat("wsp"));
       dataList.add(unit);
     
      }
      
      }catch(Exception e){
      System.out.println("Wystapil blad ;"+e);
      }
      finally {
         if (rs != null)	{
	            try {
	                rs.close();
	            } catch (Exception e) {}
	            rs = null;
	        }

	        if (stmt != null) {
	            try {
	                stmt.close();
	            } catch (Exception e) {}
	            stmt = null;
	        }
          if (currentCon != null) {

	            try {
	                currentCon.close();
	            } catch (Exception e) {
	            }
	            currentCon = null;

	        }
      }
      request.setAttribute("records",dataList); //dane do wyswietlenia na stronie w tabeli
      response.setContentType("text/html;charset=UTF-8");
      request.setCharacterEncoding("UTF-8");
      RequestDispatcher dispatcher = request.getRequestDispatcher("ListRecords.jsp");
      if (dispatcher != null)
        dispatcher.forward(request, response);
//        try {
//            em = emf.createEntityManager();
//
//            //query for all the persons in database
//            List persons = em.createQuery("select p from Person p").getResultList();
//            request.setAttribute("personList",persons);
//
//            //Forward to the jsp page for rendering
//            //request.getRequestDispatcher("ListPerson.jsp").forward(request, response);
//        } catch (Exception ex) {
//            throw new ServletException(ex);
//        } finally {
//            //close the em to release any resources held up by the persistebce provider
//            if(em != null) {
//                em.close();
//            }
//        }
      
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "ListPerson servlet";
    }
    // </editor-fold>
}
